import router from "@/router.js";

//自定义权限指令

/**
  # 权限配置文档

 ### 9.3 菜单管理：Menu

    | 操作权限 | 权限 key  |             接口地址                  |  权限指令           |
    | -------- | -------- | ------------------------------------ | ------------------ |
    | 列表     | list     | /base/manage/menu/getAllMenu                  | -                  |
    | 查看     | look     | /base/manage/menu/getMenuAndMenuAuth          | `v-auth:look`      |
    | 新建     | add      | /base/manage/menu/editMenu                    | `v-auth:add`       |
    | 修改     | edit     | /base/manage/menu/editMenu                    | `v-auth:edit`      |
    | 删除     | del      | /base/manage/menu/deleteMenuById              | `v-auth:del`       |
    | 详情     | detail   | /base/manage/menu/detail                      | `v-auth:detail`    |
    | 置顶     | top      | /base/manage/menu/toTop                       | `v-auth:top`       |
    | 启用状态 | enable   | /base/manage/menu/updateBySelective           | `$auth('enable')`  |
    | 停用状态 | disable  | /base/manage/menu/updateBySelective           | `$auth('disable')` |
 */

	console.log('=========权限配置文档============')
let menus=[
	{
		"id": "2590b88512974728847358c193a52cfe",
		"pid": "c583ed90eeae45b9aa7ab021b68f89a9",
		"title": "订单管理",
		"name": "userList",
		"icon": null,
		"url": "/aggchannel/order",
		"hide": 0,
		"keep": 0,
		"target": null,
		"auths": [
			"list",
			"detail",
            "edit",
			"del",
			"add",
            'query',
		//	'reset'
		]
	},
	{
		"id": "36c1d0c200ba40cfaa8b2d484cbe9a7f",
		"pid": "0e0222a5fb504a04ac4c7eebdcf7d749",
		"title": "客户管理",
		"name": "AggCustomerList",
		"icon": null,
		"url": null,
		"hide": null,
		"keep": null,
		"target": null,
		"auths": [
			"edit",
			"list",
			"del",
			"add"
		]
	},
	{
		"id": "61c9df630c444b298e78e404df17a888",
		"pid": "da8dbe442cb8486ca4b41b2ada158b4c",
		"title": "自有券管理",
		"name": "CouponBatchConfig",
		"icon": null,
		"url": null,
		"hide": 0,
		"keep": 0,
		"target": null,
		"auths": []
	},
	{
		"id": "6d088cc8703c47d893aad8bccd4505cf",
		"pid": "2dd588fb01cd4f408708d6fb93d1c592",
		"title": "用户管理",
		"name": "User",
		"icon": null,
		"url": null,
		"hide": 0,
		"keep": 0,
		"target": null,
		"auths": [
			"del",
			"enable",
			"list",
			"edit",
			"add",
			"disable"
		]
	},
	{
		"id": "6d088cc8703c47d893aad8bccd4505cf",
		"pid": "2dd588fb01cd4f408708d6fb93d1c592",
		"title": "商品管理",
		"name": "goodslist",
		"icon": null,
		"url": null,
		"hide": 0,
		"keep": 0,
		"target": null,
		"auths": [
			"del",
			"enable",
			"list",
			"edit",
			"add",
			"disable",
			"query",
			'reset'
		]
	}
]


//是否为超级管理员
const isSuper = () => {
  const { roles } ={
                id:"1403229184838467584",
                name:"彭文森",
                phone:"18785112455",
                roles:["不是超级管理员"]
              };
  if (Array.isArray(roles) && roles.some(s => s == '超级管理员')) return true
  return false
}

export default (Vue) => {
    /**自定义按钮权限指令 */
    Vue.directive('auth', {
      mounted(el, binding) { // 绑定元素的父组件被挂载时调用
        if (isSuper()) return true;
		console.log(binding,'==========binding==============')
        //获取按钮权限
        if (!Vue.config.globalProperties.$_auth(binding.arg)) {
          //移除不匹配的按钮
          el.parentNode.removeChild(el)
        }
      },
    })
  
    //检查权限方法
    //v-auth:add
    Vue.config.globalProperties.$_auth = function (value) {
      let name=router.currentRoute.value.name; //路由名称
	  console.log(name,'路由名称<=====================',value)
	  
      return menus.some(e => e.name === name && e.auths.includes(value)); //获取从服务器请求存储本地的按钮权限
      // console.log("不是超级管理员")  
      // let isExist = false;
      // var btnPermsArr = ['add','edit','del','query'] //获取从服务器请求存储本地的按钮权限user:add
      // if (btnPermsArr.includes(value)) {
      //   isExist = true
      // }
      // return isExist
    }
  }
